package com.ad.pages;

import java.util.Calendar;

import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Id;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.corelib.components.PasswordField;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.ad.base.AbstractPage;
import com.ad.dao.IUserDao;
import com.ad.hibernate.User;
import com.ad.services.IUserManagerService;

public class Register extends AbstractPage {

	@Property
	private String userName;

	@Property
	private String petName;

	@Property
	private String passwords;

	@Property
	private String rPassword;

	@Inject
	@Id("userDao")
	private IUserDao userDao;

	@InjectPage
	private Home index;

	@Component
	private Form regForm;

	@Component(id = "rPassword")
	private PasswordField password2Field;
	
	@Inject
	@Id("UserManagerService")
	private IUserManagerService UserManagerService;

	protected void setupRender() {
		super.setupRender();

	}

	Object onSuccessFromregForm() {
		User newUser = new User();
		newUser.setName(userName);
		newUser.setNickName(petName);
		newUser.setPass(passwords);
		Calendar c = Calendar.getInstance();
		newUser.setCreateDate(c.getTime());
		//userDao.insert(newUser);
		this.UserManagerService.insert(newUser);
		return "jump";
	}

	void onValidateFormregForm() {
		if (rPassword != null && !rPassword.equals(passwords)) {
			regForm.recordError(password2Field, "密码不一样啦");
		}
	}

}
